package com.example.teamtribe.main.club.post;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.main.club.PostingModel;
import com.example.teamtribe.utils.Utility;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PostPresenter implements PostContract.Presenter {

    private PostContract.View view;

    public PostPresenter(PostContract.View view) {
        this.view = view;
    }

    @Override
    public void post(final int teamId, final String title, final String content, final int tagId, List<String> images) {
        if (TextUtils.isEmpty(title)) {
            view.showToast("请输入标题");
            return;
        }
        List<File> files = new ArrayList<>();
        for (String string : images) {
            if (string != null) {
                files.add(new File(string));
            }
        }
        uploadImages(files, new FileModel.UploadComplete() {
            @Override
            public void complete(List<String> fileNames) {
                uploadText(teamId, title, content, tagId, fileNames);
            }
        });
        view.showProgress();

    }

    private void uploadText(int teamId, String title, String content, final int tagId, List<String> fileNames) {
        PostingModel.getInstance().post(Utility.getToken(), teamId, title, content, 0, fileNames, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                setTag(bundle.getInt("id"), tagId);
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }

    private void uploadImages(final List<File> images, final FileModel.UploadComplete complete) {
        final List<String> fileNames = new ArrayList<>();
        if (images.isEmpty()) {
            complete.complete(fileNames);
        }
        for (File image : images) {
            FileModel.getInstance().upload(Utility.getToken(), image, 1, new Back() {
                @Override
                public void success(@Nullable Bundle bundle) {
                    fileNames.add(bundle.getString("fileName"));
                    if (fileNames.size() == images.size()) {
                        complete.complete(fileNames);
                    }
                }

                @Override
                public void failure(String meg) {
                    view.hideProgress();
                    view.showToast(meg);
                }
            });
        }
    }

    private void setTag(int postId, int tagId) {
        if (tagId == 0) {
            view.hideProgress();
            view.next();
            return;
        }
        PostingModel.getInstance().setTag(Utility.getToken(), postId, tagId, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                view.hideProgress();
                view.next();
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }
}
